import sys

from PySide6 import QtWidgets
from PySide6.QtWidgets import QVBoxLayout, QFormLayout, QLineEdit, QPushButton, QMessageBox
from Qt0918.db.qt_mysql import MyMySQL

class AddStu(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.sql = MyMySQL()
        self.sql.create_table()

        self.user = {}
    def setup_ui(self):

        self.resize(400, 300)
        self.setWindowTitle("添加学生信息")
        stu_layout = QVBoxLayout()

        form_layout = QFormLayout()
        self.name_line_edit = QLineEdit()
        form_layout.addRow("姓名", self.name_line_edit)
        self.age_line_edit = QLineEdit()
        form_layout.addRow("年龄", self.age_line_edit)
        self.phone_line_edit = QLineEdit()
        form_layout.addRow("手机号", self.phone_line_edit)
        self.academic_line_edit = QLineEdit()
        form_layout.addRow("学制", self.academic_line_edit)
        self.addr_line_edit = QLineEdit()
        form_layout.addRow("学籍", self.addr_line_edit)


        add_btn = QPushButton("添加")
        add_btn.clicked.connect(self.add_stu)

        stu_layout.addLayout(form_layout)
        stu_layout.addWidget(add_btn)
        # 把垂直布局添加到窗口
        self.setLayout(stu_layout)


    def add_stu(self):
        """添加学生信息"""
        self.user.update({"username": self.name_line_edit.text(),
                     "age": self.age_line_edit.text(),
                     "phone": self.phone_line_edit.text(),
                     "academic": self.academic_line_edit.text(),
                     "addr": self.addr_line_edit.text()
                     })
        rest = self.sql.insert(**self.user)
        if rest:
            QMessageBox.information(self, "提示", "添加成功")
            self.close()
        else:
            QMessageBox.critical(self, "失败", "添加失败")



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = AddStu()
    ex.show()
    sys.exit(app.exec())